﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace ZK.YCGLRD_TransManage.Test
{
    public partial class 其他测试 : Form
    {
        private System.Timers.Timer timer = new System.Timers.Timer(100); //设置时间间隔为5秒
        private System.Timers.Timer timer1 = new System.Timers.Timer(3000); //设置时间间隔为5秒
        public 其他测试()
        {
            InitializeComponent();
            timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1EventProcessor1);
            timer1.AutoReset = false; //
            timer1.Enabled = true;
            timer1.Start();
        }

        private void timer1EventProcessor1(object sender, ElapsedEventArgs e)
        {
            //throw new NotImplementedException();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer1EventProcessor);
            timer.AutoReset = false; //
            timer.Enabled = true;
            timer.Start();
        }

        private void timer1EventProcessor(object sender, ElapsedEventArgs e)
        {
            //lock (obj)
            //{
            //    if (timer1.Enabled)
            //    {
            //        Common.Log4.LogManage.WriteInfo("timer1 is runing");

            //    }
            //    else
            //    {
            //        timer1.Start();
            //        Common.Log4.LogManage.WriteInfo("timer1 is reading");
            //    }
            //}
            ////Common.Log4.LogManage.WriteInfo("this is test timer before");
            System.Threading.Thread.Sleep(2000);
            //Common.Log4.LogManage.WriteInfo("this is test timer 1 state timer.Enabled:" + timer.Enabled);
            ////Common.Log4.LogManage.WriteInfo("this is test timer after");
            //timer.Start();
            ////Common.Log4.LogManage.WriteInfo("this is  timer start time");
            //Common.Log4.LogManage.WriteInfo("this is test timer 2 state timer.Enabled:" + timer.Enabled);

            Common.Log4.LogManage.WriteInfo("aaaaaaaaaaaaaaaaaa");
            timer.Start();


        }



        private void button2_Click(object sender, EventArgs e)
        {
            var aaa = new Action<int>(aaaaa);
            Common.Log4.LogManage.WriteInfo("Data synchronization test begain ");
            for (int i = 0; i < 20; i++)
            {
                int j = i;
                //aaa.Invoke();
                Task.Factory.StartNew(() =>
                {
                    aaaaa(j);
                });
            }
        }

        static object obj = new object();

        private void aaaaa(int i)
        {
            lock (obj)
            {
                Common.Log4.LogManage.WriteInfo("this is lock,num:" + i);
                System.Threading.Thread.Sleep(2000);
                Common.Log4.LogManage.WriteInfo("this is lock 2s num:" + i);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            Thread.Sleep(2000);
            long a = stopwatch.ElapsedMilliseconds;
            Thread.Sleep(2000);
            Thread.Sleep(200);
            long b = stopwatch.ElapsedMilliseconds;
            MessageBox.Show(b - a + "ms");
        }
    }
}
